$(document).ready(function () {

    var form=$('#form');
    var $form=new xhForm(form);
    var $type=addressUri.getParam('type');
    var $id=addressUri.getParam('id');
    var $tid=addressUri.getParam('tid');


    form.append('<input style="display: none" name="iCUId" value="'+$id+'"  />');
    form.append('<input style="display: none" name="iTrackType" value="'+$type+'"  />');
    $('body').css('background-color','#f9f9f9');
    if($type!=2){
        $(".form_datetime").datetimepicker({format: 'yyyy-mm-dd',language: 'zh-CN',minView: "month"});
    }else if($type==2) {
        $(".form-date").datetimepicker({format: 'yyyy-mm-dd',language: 'zh-CN',minView: "month", autoclose:true});
        var len=$('[name^=tTime-]').length;
        $('[name^=tTime-]').each(function (i,dom) {
            $id=$(this).attr("name").split("tTime-")[1];
            if(i == len - 1||i == len-2){
                return false;
            }
            if($id%2==1){
               $(this).css('margin-bottom','15px');
            }
        });
    }

    $(function () {//初始化
        if($type==1){
            $(".form_datetime").parent().hide();
        }else if($type==2){
            var sYear=$("[name$='sYear']");
            sYear.append('<option value="" style="display: none" selected>请选择</option>');
            for(var i=new Date().getFullYear();i>2000;i--){
                sYear.append('<option value="'+i+'">'+i+'</option>');
            }
        }else if($type==4){
            $(".form_datetime").parent().hide();
        }
        $('#fileInput').parent().hide();
        // $.ajax({
        //     url : baseUrl+'customer/findTraceData',
        //     data : {"iCUTId":$tid},
        //     dataType : 'JSON',
        //     type : 'POST',
        //     success:function (data) {
        //         $form.fill(data);
        //         form.initeValidate($('#bt_modal_confirm',parent.document));
        //     },
        //     error:function (result) {
        //         console.log(result);
        //     }
        // });
        xhExtra.ajaxRequest('customer/findTraceData',{"iCUTId":$tid},{},function (data) {
            $form.fill(data);
            form.initeValidate($('#bt_modal_confirm',parent.document));
        });
    });






    $('#bt_modal_confirm',parent.document).click(function () {
        $form.submit('customer/saveTraceData',{},function (data) {
            if($type==1){
                window.top.xhTop.getActiveFrame().frames['customer']['followUpTrace'].location.reload();
            }else if($type==2){
                window.top.xhTop.getActiveFrame().frames['customer']['intentionTrace'].location.reload();
            }else {
                window.top.xhTop.getActiveFrame().frames['customer']['operationGradeTrace'].location.reload();
            }
            window.top.xhTop.closeModal();
        });
        // $.ajax({
        //     url : baseUrl+'customer/saveTraceData',
        //     data:$("#form").serialize(),
        //     dataType:'json',
        //     type:'post',
        //     success:function (data) {
        //         if(data=='fail'){
        //             alert('内容重复');
        //             return false;
        //         }
        //         if($type==1){
        //             window.top.xhTop.getActiveFrame().frames['customer']['followUpTrace'].location.reload();
        //         }else if($type==2){
        //             window.top.xhTop.getActiveFrame().frames['customer']['intentionTrace'].location.reload();
        //         }else {
        //             window.top.xhTop.getActiveFrame().frames['customer']['operationGradeTrace'].location.reload();
        //         }
        //         window.top.xhTop.closeModal();
        //
        //     },
        //     error:function (result) {
        //         console.log(result);
        //     }
        // });

    });

    modalBind.bindClick();


});
